import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/orders",
    name: "orders",
    meta: {
      title: "订单列表",
      elIcon: "Grid"
    },
    children: [
      {
        path: "/orders",
        component: () => import("@/views/orders/index.vue"),
        name: "orders",
        meta: {
          title: "订单列表",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/erpOrders",
    redirect: "/erpOrders",
    component: Layouts,
    name: "erpOrders",
    meta: {
      title: "ERP订单列表",
      elIcon: "Grid"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/erpOrders/index.vue"),
        name: "erpOrders",
        meta: {
          title: "ERP订单列表",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/expertCommission",
    component: Layouts,
    redirect: "/expertCommission",
    name: "expertCommission",
    meta: {
      title: "达人佣金",
      elIcon: "Grid"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/expertCommission/index.vue"),
        name: "expertCommission",
        meta: {
          title: "达人佣金",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/teamLeaderServiceFee",
    component: Layouts,
    name: "teamLeaderServiceFee",
    redirect: "/teamLeaderServiceFee",
    meta: {
      title: "团长服务费",
      elIcon: "Grid"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/teamLeaderServiceFee/index.vue"),
        name: "teamLeaderServiceFee",
        meta: {
          title: "团长服务费",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/inventory",
    component: Layouts,
    name: "inventory",
    redirect: "/inventory",
    meta: {
      title: "库存表",
      elIcon: "Grid"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/inventory/index.vue"),
        name: "inventory",
        meta: {
          title: "库存表",
          keepAlive: true
        }
      },
      {
        path: "operate",
        component: () => import("@/views/inventory/operate.vue"),
        name: "opearate",
        meta: {
          title: "新增库存表",
          keepAlive: true,
          hidden: true
        }
      }
    ]
  },
  {
    path: "/dailyDetail",
    component: Layouts,
    redirect: "/dailyDetail",
    name: "dailyDetail",
    meta: {
      title: "日表明细表",
      elIcon: "Grid"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/dailyDetail/index.vue"),
        name: "dailyDetail",
        meta: {
          title: "日表明细表",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/daily",
    component: Layouts,
    redirect: "/daily",
    name: "daily",
    meta: {
      title: "日表汇总表",
      elIcon: "Grid"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/daily/index.vue"),
        name: "daily",
        meta: {
          title: "日表汇总表",
          keepAlive: true
        }
      }
    ]
  },
  {
    path: "/systemManage",
    component: Layouts,
    redirect: "/systemManage/users",
    name: "users",
    meta: {
      title: "系统管理",
      elIcon: "Tools"
    },
    children: [
      {
        path: "users",
        component: () => import("@/views/systemManage/users/index.vue"),
        name: "users",
        meta: {
          title: "用户列表",
          keepAlive: true
        }
      },
      {
        path: "users/operate",
        component: () => import("@/views/systemManage/users/operate.vue"),
        name: "usersOperate",
        meta: {
          title: "新增用户",
          keepAlive: true,
          hidden: true
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
